package contact.view;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;


import model.db.Query;
import model.db.User;
import model.db.Vertical;
import model.db.VerticalResult;

import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.hibernate.HibernateException;
import org.hibernate.Session;


import contact.controller.QueryManager;
import contact.controller.UserManager;
import contact.controller.VerticalManager;
import contact.controller.VerticalResultsManager;
import contact.model.Contact;
import contact.util.HibernateUtil;
import contact.view.util.SurveyLogic;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction  extends ActionSupport implements SessionAware,ServletContextAware {

	private static final long serialVersionUID = 9149822260758390091L;
	
	
	



	private User user ;
	private Query q;
	private Vertical left_vertical;
	private Vertical right_vertical;
	
	private String result_type_left=null;
	private String result_type_right=null;
	
	

	public String getResult_type_right() {
		
		result_type_right= SurveyLogic.classifyResultType(this.right_vertical);
		
		return result_type_right;
	}

	public void setResult_type_right(String result_type_right) {
		this.result_type_right = result_type_right;
	}

	

	public String getResult_type_left() {
		
		result_type_left= SurveyLogic.classifyResultType(this.left_vertical);
		
		return result_type_left;
	}

	public void setResult_type_left(String result_type_left) {
		this.result_type_left = result_type_left;
	}

	public Query getQ() {
		return q;
	}

	public Vertical getLeft_vertical() {
		return left_vertical;
	}

	public void setLeft_vertical(Vertical left_vertical) {
		this.left_vertical = left_vertical;
	}

	public Vertical getRight_vertical() {
		return right_vertical;
	}

	
	
	public void setRight_vertical(Vertical right_vertical) {
		this.right_vertical = right_vertical;
	}

	public void setQ(Query q) {
		this.q = q;
	}

	

	public List<VerticalResult> getLeft_results() {
		return left_results;
	}

	public void setLeft_results(List<VerticalResult> left_results) {
		this.left_results = left_results;
	}

	public List<VerticalResult> getRight_results() {
		return right_results;
	}

	public void setRight_results(List<VerticalResult> right_results) {
		this.right_results = right_results;
	}



	private List<VerticalResult> left_results;
	private List<VerticalResult> right_results;
	
	private Long id;

	 // For SessionAware
	Map<String, Object> session;

	
	private UserManager userManager;
	private QueryManager queryManager;
	private VerticalManager verticalManager;
	private VerticalResultsManager verticalResultsManager;
	private ServletContext servletContext;
	
	public LoginAction() {
		
		
		
	
		
		System.out.println("Inside constructor of LoginAction");
		userManager= new UserManager();
		queryManager= new QueryManager();
		
		verticalManager= new VerticalManager();
		
		verticalResultsManager= new VerticalResultsManager();
		
	}
	
	

	private void initVerticalInfo() {
		// TODO Auto-generated method stub
		
		if(this.servletContext.getAttribute(SurveyLogic.VERTICALS_INFO)==null){
			
			SurveyLogic.loadVerticalInfo(servletContext, verticalManager);
		}	
		
	}

	public String execute() {
		
		System.out.println(getUser());
	
		
		
		
		try {
			
			
		//	if(session.containsKey(SurveyLogic.LOGIN) && session.containsKey(SurveyLogic.USER)){
				
			//	if(session.get(SurveyLogic.USER)!=null&& session.get(SurveyLogic.LOGIN) ==SurveyLogic.LOGIN_ACTIVE ){
					
				//	return SUCCESS;
			//	}
		//	}
			if(getUser()!=null  && getUser().getGender()!=null){
				
				userManager.add(getUser());
			}else{
				
				if(session.containsKey(SurveyLogic.USER)){
					
					this.user= (User) session.get(SurveyLogic.USER);
					
				}else{
					return  SurveyLogic.ACTION_LOGIN;
					
				}
			}
			
			//init query
			if( !session.containsKey(SurveyLogic.SESSION_QUERY)){
				 q= SurveyLogic.getNextQuery(servletContext, queryManager, session);
				 
				 
				 session.put(SurveyLogic.SESSION_QUERY,q);
				 initVerticalInfo();
				
				 
					
			}else{
				
				
				q= (Query) session.get(SurveyLogic.SESSION_QUERY);
			}
			
			System.out.println("Login Action execute metod ---------");
			
			System.out.println("Printing query info");
			
			
			System.out.println("----" + "\t"+ q.getQuery());
			
			
		
			
			
		//	SurveyLogic.printQueryList(servletContext);
		//	SurveyLogic.printQueryCounts(servletContext);
			
			this.session.put(SurveyLogic.USER, getUser());
			
			
			System.out.println("user info:" + getUser().getGender() + "\t"+ getUser().getAge());
			
			this.session.put(SurveyLogic.LOGIN, SurveyLogic.LOGIN_ACTIVE);
			this.session.put(SurveyLogic.SESSION_QUERY, q);
			
			if(session.containsKey(SurveyLogic.SESSION_VERTICAL_HASH) && 
					session.containsKey(SurveyLogic.SESSION_LEFT_RESULTS) &&
					
					session.containsKey(SurveyLogic.SESSION_RIGHT_RESULTS)
			){
				
				this.left_results= (List<VerticalResult>) session.get(SurveyLogic.SESSION_LEFT_RESULTS);
				this.right_results= (List<VerticalResult>) session.get(SurveyLogic.SESSION_RIGHT_RESULTS);
				
				SurveyLogic.assignCurrentVerticalDescriptions(servletContext,session,left_results,right_results);
				
						
				
				this.left_vertical= (Vertical) servletContext.getAttribute(SurveyLogic.LEFT_VERTICAL);
				this.right_vertical= (Vertical) servletContext.getAttribute(SurveyLogic.RIGHT_VERTICAL);
	
				return SUCCESS;
				
			}else{
				SurveyLogic.getNextVerticalResults(q.getId(), verticalResultsManager, session,SurveyLogic.AGAINTS_GOOGLE);
			}
			
			left_results= (ArrayList<VerticalResult>) session.get(SurveyLogic.SESSION_LEFT_RESULTS);
			right_results = (ArrayList<VerticalResult>) session.get(SurveyLogic.SESSION_RIGHT_RESULTS);
			
			SurveyLogic.assignCurrentVerticalDescriptions(servletContext,session,left_results,right_results);
			
				
			
			left_vertical= (Vertical) session.get(SurveyLogic.LEFT_VERTICAL);
			right_vertical= (Vertical)session.get(SurveyLogic.RIGHT_VERTICAL);
			
			
			SurveyLogic.printVerticalInfo(servletContext);
			
			SurveyLogic.printLeftWebResult(session);
			
			
			
		
			System.out.println("End of LoginAction execute ------------");

		} catch (Exception e) {
			e.printStackTrace();
		}
	
		
		return SUCCESS;
	}

	public User getUser() {
		return user;
	}



	public void setUser(User user) {
		this.user =user;
	}

	

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		System.out.println("INside set session loginaction");
	    this.session = session;
	  }

	@Override
	public void setServletContext(ServletContext arg0) {
		// TODO Auto-generated method stub
		
		
		this.servletContext=arg0;
	}
	
	
}
